package org.dromara.quick.chat.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.quick.chat.model.bo.create.ImGroupChatCreateBo;
import org.dromara.quick.chat.model.bo.query.ImGroupChatBo;
import org.dromara.quick.chat.service.ImGroupChatService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Stars
 * @description 针对表【im_group_chat(群聊表（群通讯录），记录群聊的相关信息及设置)】的数据库操作Service
 * @Date: 2025/3/6 22:32
 */
@Tag(name = "群聊表（群通讯录）")
@RequiredArgsConstructor
@RestController
@RequestMapping("im/group/chat")
public class ImGroupChatController extends BaseController {

    private final ImGroupChatService imGroupChatService;

    /**
     * 聊天框-查询群基本信息
     */
    @Operation(summary = "聊天框-查询群基本信息")
    @GetMapping("/info/{groupId}")
    public R<?> selectGroupChatInfo(@PathVariable("groupId") Long groupId) {
        if (groupId == null){
            return R.fail("群编号不能为空");
        }
        return R.ok(imGroupChatService.selectGroupChatInfo(groupId,LoginHelper.getUserId()));
    }

    /**
     * 通讯录-群聊列表
     */
    @Operation(summary = "通讯录-群聊列表")
    @GetMapping("/list")
    public R<?> selectGroupChatList(ImGroupChatBo imGroupChatBo) {
        return R.ok(imGroupChatService.selectGroupChatList(imGroupChatBo));
    }

    /**
     * 根据群号搜索群
     */
    @Operation(summary = "根据群号搜索群")
    @GetMapping("/search")
    public R<?> searchGroupChat(Long id,String groupNumber) {
        return R.ok(imGroupChatService.searchGroupChat(id,groupNumber));
    }


    /**
     * 创建群
     * @param imGroupChatCreateBo
     * @return
     */
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    @Operation(summary = "创建群")
    @PostMapping("/create")
    public R<?> createGroupChat(@RequestBody ImGroupChatCreateBo imGroupChatCreateBo) throws IOException {
        imGroupChatService.createGroupChat(imGroupChatCreateBo);
        return R.ok();
    }
}
